import Global from 'system.global';
export function LoadJS(id: string, fileUrl: string) {
  var scriptTag = document.getElementById(id);
  if (scriptTag) {
    return;
  }
  var oHead: any = document.getElementsByTagName("HEAD").item(0);
  //  oHead.removeChild(scriptTag);
  var oScript = document.createElement("script");
  oScript.id = id;
  oScript.type = "text/javascript";
  oScript.src = fileUrl;
  oHead.appendChild(oScript);
}

export function whichElement(e: any) {
  var targ
  if (!e) e = window.event
  if (e.target) targ = e.target
  else if (e.srcElement) targ = e.srcElement
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode
  return targ;
}

export function dataset(element:any) {
  var obj:{[key:string]:any} = {};
  if (element.dataset) {
    return element.dataset;
  } else {
    for (var i = 0; i < element.attributes.length; i++) {
      var key = element.attributes[i].nodeName;
      if (/^data-\w+$/.test(key)) {//判断是否以data-开头的属性名
        var value = element.attributes[i].nodeValue;//值
        var keyName = key.match(/^data-(\w+)/)[1];//键名
        obj[keyName] = value;//对象添加属性
      }
    }
  }
  return obj;
}

const audioObj = new Audio();
const g = Global();
let playAudio:number = -1;
audioObj.onended=function(){
  playAudio = -1;
}


type AudioType = "default";

const audioEnum = {
  default:g.path+'/assets/fire.mp3'
}



const level = {
  default:0
}


export function play(type:AudioType){
  if(level[type] <=　playAudio){
    return;
  }
  audioObj.src = audioEnum[type];
  audioObj.play();
}